﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DaphneProfiBase;
using GalaSoft.MvvmLight.Messaging;

namespace DaphneControls
{
    /// <summary>
    /// Interaction logic for GameLibraryControl.xaml
    /// </summary>
    public partial class GameLibraryControl : UserControl
    {
        public GameLibraryControl()
        {
            InitializeComponent();

            // register event when loaded
            this.Loaded += GameLibraryControl_Loaded;

            // Register message for generating columns
            Messenger.Default.Register<NotificationMessage<List<string>>>(this, (nm) =>
            {
                if (nm.Notification == Notify.PDNGameLibGenerateColumns)
                {
                    GenerateColumns(nm.Content);
                }
            });

            // register message for adding columns
            Messenger.Default.Register<NotificationMessage<string>>(this, (nm) =>
                {
                    if (nm.Notification == Notify.PDNGameLibAddColumn)
                    {
                        AddColumn(nm.Content);
                    }
                });

            // register message for deleting columns
            Messenger.Default.Register<NotificationMessage<string>>(this, (nm) =>
                {
                    if (nm.Notification == Notify.PDNGameLibDelColumn)
                    {
                        DeleteColumn(nm.Content);
                    }
                });

            // register message for refreshing UI
            Messenger.Default.Register<NotificationMessage>(this, (nm) =>
                {
                    if (nm.Notification == Notify.InvalidateVisual && nm.Sender == this.DataContext)
                    {
                        uxPDNGameList.Items.Refresh();
                    }
                });
        }

        void GameLibraryControl_Loaded(object sender, RoutedEventArgs e)
        {
            // send message to viewmodel
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(Notify.PDNGameLibLoaded));
        }

        /// <summary>
        /// Generates the columns
        /// </summary>
        private void GenerateColumns(List<string> lstColumns)
        {
            // clear all columns
            uxPDNGameList.Columns.Clear();

            // generate columns
            foreach (string pdnTag in lstColumns)
            {
                AddColumn(pdnTag);
            }
        }

        /// <summary>
        /// Adds the column
        /// </summary>
        /// <param name="pdnTag"></param>
        private void AddColumn(string pdnTag)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            column.Header = pdnTag;
            column.Binding = new Binding(pdnTag);
            column.IsReadOnly = true;

            uxPDNGameList.Columns.Add(column);
        }

        /// <summary>
        /// Deletes the specific column
        /// </summary>
        private void DeleteColumn(string pdnTag)
        {
            foreach (var col in uxPDNGameList.Columns)
            {
                if ((col.Header as string) == pdnTag)
                {
                    uxPDNGameList.Columns.Remove(col);
                    break;
                }
            }
        }
    }
}
